package com.hj.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

//全局异常处理器
public class CustomExceptionResolver implements HandlerExceptionResolver {

	//系统抛出的异常
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		// TODO Auto-generated method stub
		//handler就是处理器适配周期要执行的Handler对象
		//解析出异常类型		
		CustomException customException=null;
		if(ex instanceof CustomException){
			//如果是系统自定义的有异常，直接取出异常信息，在错误页面显示
			customException=(CustomException) ex;
		}else{
			//不是系统自定义的异常
			customException=new CustomException("未知错误");
		}
		String message=customException.getMessage();
		ModelAndView modelAndView=new ModelAndView();
		//将错误信息传到页面
		modelAndView.addObject("message", message);
		modelAndView.setViewName("err");
		return modelAndView;
	}

}
